#-*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
import os
from zipfile import ZipFile
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

class Spider1pptJieShaoPPT():
    def __init__(self):
        self.host = 'https://www.1ppt.com'
        self.url = self.host + '/xiazai/jieshao/'
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Host': 'www.1ppt.com',
            'Referer': 'https://www.1ppt.com/',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': UserAgent().chrome
        }
        self.file_output_path = './document/1ppt/'

    def __main__(self):
        print('第1PPT网自我介绍PPT文件批量下载爬虫开始执行，请稍后。。。')
        response = requests.get(self.url, self.headers)
        bs = BeautifulSoup(response.content.decode(errors='ignore'), 'lxml')
        ppt_elements = bs.select('.tplist > li')
        num = 0
        for ppt_element in ppt_elements:
            # 打开ppt详情页面
            ppt_info_url = self.host + ppt_element.select_one('li > a')['href']
            # 开启无界面模式
            options = webdriver.ChromeOptions()
            options.add_argument("--headless")
            options.add_argument("--disable-gpu")

            driver = webdriver.Chrome(chrome_options=options)
            driver.get(url=ppt_info_url)
            driver.implicitly_wait(3)
            ppt_info_element = driver.find_element(by=By.CLASS_NAME, value='ppt_info')
            ppt_name = ppt_info_element.find_element(by=By.TAG_NAME, value='h1').text
            # 点击进入第一PPT素材下载页面
            driver.find_element(by=By.CLASS_NAME, value='downurllist').\
            find_elements(by=By.TAG_NAME, value='li')[0].find_element(by=By.TAG_NAME, value='a').click()
            # 切换到下载页面
            current_windows = driver.window_handles
            driver.switch_to.window(current_windows[1])
            # 下载
            self.__create_dir__(self.file_output_path)
            ppt_download_url = driver.find_element(by=By.CLASS_NAME, value='downloadlist').find_elements(by=By.TAG_NAME, value='li')[0].\
                find_element(by=By.TAG_NAME, value='a').get_attribute('href')
            ppt_zip_name = ppt_download_url[ppt_download_url.rfind('/'):]
            with open(self.file_output_path + ppt_zip_name, 'wb') as file:
                file.write(requests.get(ppt_download_url).content)
            file.close()
            # 解压
            with ZipFile(self.file_output_path + ppt_zip_name, allowZip64=True) as read_zip:
                # 排除目录文件
                # file_iter = (file for file in read_zip.filelist if not file.is_dir())
                for file in read_zip.filelist:
                    # 编码文件名称为 utf 格式
                    try:
                        file.filename = file.filename.decode('utf-8')
                    except:
                        file.filename = file.filename.encode('437').decode('gbk')
                        file.filename = file.filename.encode('utf-8').decode('utf-8')
                    if file.filename.find('.pptx') > 0:
                        ppt_name = ppt_name + '.' + file.filename.split('.')[-1]
                        file.filename = ppt_name
                        read_zip.extract(file, self.file_output_path)
                        print('PPT模板----{}-----下载完成'.format(ppt_name))
            read_zip.close()
            os.remove(self.file_output_path + ppt_zip_name)
            time.sleep(3)
            driver.quit()


    def __create_dir__(self, dir):
        # 获取倒数第二个/出现的位置
        last_sec_num = dir.rfind('/', 0, dir.rfind('/')-1)
        # dir的上一级目录
        dir_parent = dir[0:last_sec_num+1]
        # 判断该文件夹是否存在
        if os.path.exists(dir_parent) is False:
            # 不存在则创建
            os.mkdir(dir_parent)
            os.mkdir(dir)
        elif os.path.exists(dir) is False:
            os.mkdir(dir)

if __name__ == '__main__':
    spider = Spider1pptJieShaoPPT()
    spider.__main__()
